package pl.veneo.wigprofit.view.elements {
	import pl.veneo.wigprofit.ctrl.events.ChartResizeEvent;
	import pl.veneo.wigprofit.tools.TimeSpan;
	import pl.veneo.wigprofit.model.PeriodData;
	import pl.veneo.wigprofit.ctrl.events.SliderUpdateEvent;
	import flash.geom.Rectangle;
	import flash.events.MouseEvent;
	import pl.veneo.wigprofit.model.Configuration;
	import flash.display.Sprite;

	/**
	 * @author Tomasz Swierszcz, fiodorkrk@gmail.com
	 */
	public class SliderBar extends Sprite {
		
		private var _width : Number;
		private var _height : Number;
		private var _bar: Sprite;
		private var _left: Sprite;
		private var _right: Sprite;
		private var _period: PeriodData;
		
		public var initialized: Boolean = false;

		public function SliderBar(width : Number, height : Number) {
			_height = height;
			_width = width;
			
			graphics.lineStyle(1,Configuration.getInstance().lightColor);
			graphics.moveTo(-5, 5);
			graphics.lineTo(_width+5, 5);
			
			_bar = addChild(new Sprite()) as Sprite;
			_bar.graphics.lineStyle(1,Configuration.getInstance().lightColor,1,true);
			_bar.graphics.beginFill(0xFFFFFF);
			_bar.graphics.drawRect(0, 0, _width, 10);
			_bar.addEventListener(MouseEvent.MOUSE_DOWN, onBarStartMove);
			
			_right = addChild(new Sprite()) as Sprite;
			_right = addChild(new Sprite()) as Sprite;
			_right.graphics.beginFill(0xFF0000,0);
			_right.graphics.drawRect(-10, 0, 20, 10);
			_left = addChild(new Sprite()) as Sprite;
			_left.graphics.beginFill(0xFF0000,0);
			_left.graphics.drawRect(-10, 0, 20, 10);
			
			_left.x = 0;
			_right.x = _width;
			
			_left.useHandCursor = true;
			_left.buttonMode = true;
			_left.addEventListener(MouseEvent.MOUSE_DOWN, onBeginResize);
			
			_right.useHandCursor = true;
			_right.buttonMode = true;
			_right.addEventListener(MouseEvent.MOUSE_DOWN, onBeginResize);
			
		}
		
		public function init(period: PeriodData) : void {
			
			initialized = true;
			_period = period;
			
		}
		
		private function updateView(): void {
			
			dispatchEvent(new SliderUpdateEvent(SliderUpdateEvent.ON_SLIDER_CHANGE, _bar.x/(_width-_bar.width), Math.round(_bar.scaleX*100)/100));
			
		}
		
		private function updatePeriod(): void {
			
			dispatchEvent(new ChartResizeEvent(ChartResizeEvent.ON_CHART_RESIZE, getDateForPositionX(_left.x),getDateForPositionX(_right.x)));
			
		}
		
		private function getDateForPositionX(posX: Number) : Date {
			
			var startDay: Number = _period.startDay.valueOf();
			var days: Number = Math.floor(posX / _width * _period.days);
			var miliseconds : Number = days * TimeSpan.MILLISECONDS_IN_DAY;
			var date: Date = new Date();
			date.setTime(startDay+miliseconds);
			return date;
			
		}

		private function onBarStartMove(event : MouseEvent) : void {
			
			_bar.startDrag(false, new Rectangle(0,0,_width-_bar.width,0));
			_bar.addEventListener(MouseEvent.MOUSE_UP, endBarMove);
			_bar.addEventListener(MouseEvent.MOUSE_MOVE, onBarMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, endBarMove);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onBarMove);
			
		}

		private function onBarMove(event : MouseEvent) : void {
			
			_left.x = _bar.x;
			_right.x = _bar.x + _bar.width;
			updateView();
			
		}

		private function endBarMove(event : MouseEvent) : void {
			
			_bar.stopDrag();
			_bar.removeEventListener(MouseEvent.MOUSE_UP, endBarMove);
			_bar.removeEventListener(MouseEvent.MOUSE_MOVE, onBarMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP, endBarMove);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onBarMove);
			updatePeriod();
			
		}

		private function onBeginResize(event : MouseEvent) : void {
			
			var slider: Sprite = event.target as Sprite;
			slider.addEventListener(MouseEvent.MOUSE_UP, endResize);
			slider.addEventListener(MouseEvent.MOUSE_MOVE, onResize);
			stage.addEventListener(MouseEvent.MOUSE_UP, endResize);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onResize);
			switch(slider){
				case _left:
				_left.startDrag(true,new Rectangle(0,0,_right.x - 30,0));
				break;
				case _right:
				_right.startDrag(true,new Rectangle(_left.x + 30,0,_width - _left.x - 30,0));
				break;
			}
			
		}

		private function onResize(event : MouseEvent) : void {
			
			_bar.x = _left.x;
			_bar.width = _right.x - _left.x;
			updateView();
			
		}

		private function endResize(event : MouseEvent) : void {
			
			_left.stopDrag();
			_left.removeEventListener(MouseEvent.MOUSE_UP, endResize);
			_left.removeEventListener(MouseEvent.MOUSE_MOVE, onResize);
			_right.stopDrag();
			_right.removeEventListener(MouseEvent.MOUSE_UP, endResize);
			_right.removeEventListener(MouseEvent.MOUSE_MOVE, onResize);
			stage.removeEventListener(MouseEvent.MOUSE_UP, endResize);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onResize);
			updatePeriod();
			
		}
	}
}
